//
//  KBWelcomeViewController.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/18.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit
import SDWebImage

class KBWelcomeViewController: UIViewController {

    // MARK: 属性
    private var iconViewBottomConstaint: NSLayoutConstraint?
    
    
    // MARK: 方法
    override func viewDidLoad() {
        super.viewDidLoad()

        //创建UI
        setupUI()
        
        //先显示上次头像
        setupIcon()
        
        //显示现在用户头像
        KBUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            
            if error != nil {
                print("-- \(error)")
                return
            }
            
            self.setupIcon()
        })
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewBottomConstaint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        //让头像动画显示
        //usingSpringWithDamping:弹簧效果0~1
        //initialSpringVelocity:初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //闭包内不能省略self
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
         
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    self.welcomeLabel.hidden = false
                    }, completion: { (_) -> Void in
                        AppDelegate.switchRootViewController(true)
                })
        }
    }

    //创建UI
    private func setupUI() {
        
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //添加约束        
        /* 背景 */
        bgImageView.ff_Fill(view)
        
        /* 头像 */
        let constaints = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        //要取那个view的约束 ，就用那个view去调用
        //NSLayoutConstraint: 这个view上的所有约束
        //attribute: 要取的约束
        iconViewBottomConstaint = iconView.ff_Constraint(constaints, attribute: NSLayoutAttribute.Bottom)
        
        /* 文字 */
        welcomeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
    
    //设置头像图片
    private func setupIcon() {
        if let avatar_large = KBUserAccount.loadUserAccount()?.avatar_large {
            let url = NSURL(string: avatar_large)
            
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big")) 
        }
    }
    
    // MARK: 懒加载
    /* 背景 */
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /* 头像 */
    private lazy var iconView: UIImageView = {
        
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
       
        iconView.layer.cornerRadius = 42.5//变圆

        iconView.clipsToBounds = true
        
        return iconView
    }()
    
    /* 文字 */
    private lazy var welcomeLabel: UILabel = {
    
        var label = UILabel()
        
        label.text = "欢迎归来"
        
        label.textColor = UIColor.blackColor()
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.alpha = 0
        
        label.hidden = true
        
        return label
    }()
}
